package com.dhz.common.redis;

import org.apache.commons.io.IOUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class RedisServerConf {

    /** Redis 服务器地址，必填*/
    private String host = "localhost";
    /** Redis 服务器端口，必填 */
    private int port = 6379;
    /** Redis 服务访问密码，有则必填 */
    private String password = null;
    /** Redis 数据库物理序号，默认是0，非必填 */
    private int database = 0;
    /** 链接超市时间，默认是0，非必填 */
    private int timeout = 2000;

    /** 连接池配置 */
    private JedisPoolConfig poolConfig;

    /** lua脚本sha值 */
    private String luaSha;
    /** lua脚本加载路径 */
    private String luaFilePath;
    /** 权重值 */
    private int weight;
    /** 当前权重 */
    private int currentWeight;
    /** 有效权重值 */
    private int effectiveWeight;
    /** Jedis连接池 */
    private JedisPool pool;
    /** 是否已关闭 */
    private boolean down;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getDatabase() {
        return database;
    }

    public void setDatabase(int database) {
        this.database = database;
    }

    public JedisPoolConfig getPoolConfig() {
        return poolConfig;
    }

    public void setPoolConfig(JedisPoolConfig poolConfig) {
        this.poolConfig = poolConfig;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public String getLuaSha() {
        return luaSha;
    }

    public void setLuaSha(String luaSha) {
        this.luaSha = luaSha;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getCurrentWeight() {
        return currentWeight;
    }

    public void setCurrentWeight(int currentWeight) {
        this.currentWeight = currentWeight;
    }

    public int getEffectiveWeight() {
        return effectiveWeight;
    }

    public void setEffectiveWeight(int effectiveWeight) {
        this.effectiveWeight = effectiveWeight;
    }

    public JedisPool getPool() {
        return pool;
    }

    public void setPool(JedisPool pool) {
        this.pool = pool;
    }

    public boolean isDown() {
        return down;
    }

    public void setDown(boolean down) {
        this.down = down;
    }

    public String getLuaFilePath() {
        return luaFilePath;
    }

    public void setLuaFilePath(String luaFilePath) {
        this.luaFilePath = luaFilePath;
    }

    /**
     * 加载Lua脚本文件并载入到redis instance
     * @throws IOException 抛出异常
     */
    public void loadLuaScript() throws IOException {
        if(getLuaFilePath() != null && !"".equals(getLuaFilePath())) {
            Jedis jedis = this.getPool().getResource();

            InputStream is = null;

            try {
                is = new FileInputStream(new File(getClass().getResource("/").getPath() + this.getLuaFilePath()));
                this.setLuaSha(jedis.scriptLoad(IOUtils.toString(is, "UTF-8")));
            } finally {
                if (is != null) {
                    is.close();
                }
                this.getPool().returnResourceObject(jedis);
            }
        }
    }

}
